package org.shadow.shapac;

import java.io.Serializable;

public class Maze implements Serializable {

	private static final long serialVersionUID = 1L;

	public static final int UP = 0, DOWN = 1, RIGHT = 2, LEFT = 3;

	private boolean[][] verticalLines;
	private boolean[][] horizontalLines;
	private int sizeX, sizeY; // stores the width and height of the maze
	private int currentX, currentY; // stores the current location of the ball
	private int finalX, finalY; // stores the ending of the maze
	private boolean gameover;

	private int inkyX, inkyY;
	private int pinkyX, pinkyY;

	public int getMazeWidth() {
		return sizeX;
	}

	public int getMazeHeight() {
		return sizeY;
	}

	public boolean move(int direction) {
		boolean moved = false;
		if (direction == UP) {
			if (currentY != 0 && !horizontalLines[currentY - 1][currentX]) {
				currentY--;
				moved = true;
			}
		}
		if (direction == DOWN) {
			if (currentY != sizeY - 1 && !horizontalLines[currentY][currentX]) {
				currentY++;
				moved = true;
			}
		}
		if (direction == RIGHT) {
			if (currentX != sizeX - 1 && !verticalLines[currentY][currentX]) {
				currentX++;
				moved = true;
			}
		}
		if (direction == LEFT) {
			if (currentX != 0 && !verticalLines[currentY][currentX - 1]) {
				currentX--;
				moved = true;
			}
		}
		if (moved) {
			if ((currentX == inkyX && currentY == inkyY)
					|| (currentX == pinkyX && currentY == pinkyY))
				gameover = true;
		}
		return moved;
	}

	public void inkyMove() {
		int random;
		random = (int) (100 * Math.random() % 4);
		switch (random) {
		case UP:
			if (inkyY != 0 && !horizontalLines[inkyY - 1][inkyX]) {
				inkyY--;
			}
			break;
		case DOWN:
			if (inkyY != sizeY - 1 && !horizontalLines[inkyY][inkyX]) {
				inkyY++;
			}
			break;
		case RIGHT:
			if (inkyX != inkyX - 1 && !verticalLines[inkyY][inkyX]) {
				inkyX++;
			}
			break;
		case LEFT:
			if (inkyX != 0 && !verticalLines[inkyY][inkyX - 1]) {
				inkyX--;
			}
			break;
		default:
		}
	}

	public void pinkyMove() {
		int random;
		random = (int) (100 * Math.random() % 4);
		switch (random) {
		case UP:
			if (pinkyY != 0 && !horizontalLines[pinkyY - 1][pinkyX]) {
				pinkyY--;
			}
			break;
		case DOWN:
			if (pinkyY != sizeY - 1 && !horizontalLines[pinkyY][pinkyX]) {
				pinkyY++;
			}
			break;
		case RIGHT:
			if (pinkyX != pinkyX - 1 && !verticalLines[pinkyY][pinkyX]) {
				pinkyX++;
			}
			break;
		case LEFT:
			if (pinkyX != 0 && !verticalLines[pinkyY][pinkyX - 1]) {
				pinkyX--;
			}
			break;
		default:
		}
	}

	public boolean isGameOver() {
		return gameover;
	}

	public void setStartPosition(int x, int y) {
		currentX = x;
		currentY = y;
	}

	public void setInkyStartPosition(int x, int y) {
		inkyX = x;
		inkyY = y;
	}

	public void setPinkyStartPosition(int x, int y) {
		pinkyX = x;
		pinkyY = y;
	}

	public int getFinalX() {
		return finalX;
	}

	public int getFinalY() {
		return finalY;
	}

	public void setFinalPosition(int x, int y) {
		finalX = x;
		finalY = y;
	}

	public int getCurrentX() {
		return currentX;
	}

	public int getCurrentY() {
		return currentY;
	}

	public int getInkyX() {
		return inkyX;
	}

	public int getInkyY() {
		return inkyY;
	}

	public int getPinkyX() {
		return pinkyX;
	}

	public int getPinkyY() {
		return pinkyY;
	}

	public boolean[][] getHorizontalLines() {
		return horizontalLines;
	}

	public void setHorizontalLines(boolean[][] lines) {
		horizontalLines = lines;
		sizeX = horizontalLines[0].length;
	}

	public boolean[][] getVerticalLines() {
		return verticalLines;
	}

	public void setVerticalLines(boolean[][] lines) {
		verticalLines = lines;
		sizeY = verticalLines.length;
	}
}
